<?php
	if (!session_id()) session_start();
	if(!isset($_SESSION['user_id'])) {
		 header("Location: user_login");
		 exit;
	}
?>
<?php
	require_once('header.php');
	require_once('navbar.php');
	require_once('configure/db_fns.php');
	require_once('configure/parameter.php');
	require_once('paganation/page.class.php');

	
?>
<?php
	$order_type = isset($_GET['type'])?" and state='".$_GET['type']."'":" and state='2'";
	$order_num = isset($_GET['type'])?$_GET['type']:'2';
	$conn = db_connect();
	$conn->query("set names utf8");
	$result = $conn->query("select * from ".$users_info." where user_id='".$_SESSION['user_id']."'");
	$row = $result->fetch_assoc();
	//分页参数
	$showrow = 12; //一页显示的行数
	$curpage = empty($_GET['page']) ? 1 : $_GET['page']; //当前的页,还应该处理非数字的情况
	
	
	//去掉当前url中的page
	$current_url = $_SERVER['QUERY_STRING'];
	$str_ex = explode("&",$current_url);
	if(strstr($current_url,"page")===false) {
		$url = "?".$current_url."&page={page}";
	}
	else {
		$str_page = preg_replace("/(\&page=\d+)/","&page={page}",$current_url);//已经存在page的情况下将page进行替换
		$url = "?".$str_page;
	}
	$query = "select order_number,car_id,state,appointment_time from ".$orders_table." where user_id='".$_SESSION['user_id']."'".$order_type." order by id desc";
	$result1 = $conn->query($query);

	$total=$result1->num_rows;//记录总条数
	if($total!=0) {
		while($row1 = $result1->fetch_assoc()) {
			
		}
	}
	//$total = mysql_num_rows(mysql_query($sql)); 
	if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow))
		$curpage = ceil($total / $showrow); //当前页数大于最后页数，取最后一页
	//获取数据
	$query .= " LIMIT " . ($curpage - 1) * $showrow . ",$showrow;";
	$result1 = $conn->query($query);
	
?>
<script>
	$(function(){
		//提示语
		 $(".waring").hover(function () {
		 	$(this).siblings(".prompt").toggle();
		 });
		
		});
	function cancel_order(car_id,bt) {
		var r = confirm("确认取消预约吗？");
		if(r==true) {
			//取消预约
			$.ajax({
			type:"POST",
			url:"configure/cancel_order.php",
			cache:false,
			data:{
				"car_id":car_id,
			 },
			beforeSend:function(){
				$(bt).html("取消中...");
				
			},
			success:function(data){
				if(data==3) {
					show_model(0);
				}
				else if(data==2) {
					content+= '<p class="helpbuy-demand-success helpbuy-demand-wrong">取消失败，请刷新后重试!</p>';
				    show_clue(content);
				}
				else if(data==1) {
					window.location.reload();
				}
				$(bt).html("取消预约");
			},
			})
		}
		else {
		}
	}
	
</script>
<!--主体部分-->
 <div class="usercenter-box">
            <div class="usercenter-box-left">
                <div class="usercenter-box-left-face">
                    <div class="usercenter-box-left-faceimg"><img src="<?php echo $row['img_src']==""?'images/askface.png':$row['img_src'];?>" /></div>
                    <div class="usercenter-box-left-font"><?php echo $row['nickname']==""?$row['user_id']:$row['nickname']; ?></div>
                </div>
                
                <div class="usercenter-box-left-ul">
                    <ul>
                    	<?php
							require_once('usercenter_navbar.php');
						?>
                    </ul>

                </div>
            </div>
            
            <div class="usercenter-box-right" id="tab">
                 <ul class="tab_menu">
                 	
                    <a href="<?php echo $SER_ADD;?>/user_order?type=2"><li class="useleft <?php echo $order_num=="2"?'useselected':''; ?>">待处理</li></a>
                    <a href="<?php echo $SER_ADD;?>/user_order?type=4"><li class="<?php echo $order_num=="4"?'useselected':''; ?>">预约成功</li></a>
                    <a href="<?php echo $SER_ADD;?>/user_order?type=7"><li class="<?php echo $order_num=="7"?'useselected':''; ?>">已付订金</li></a>
                    <a href="<?php echo $SER_ADD;?>/user_order?type=5"><li class="<?php echo $order_num=="5"?'useselected':''; ?>">交易成功</li></a>
                    <a href="<?php echo $SER_ADD;?>/user_order?type=6"><li class="<?php echo $order_num=="6"?'useselected':''; ?>">交易关闭</li></a>
                    <!--<li>待付款</li>
                    <li>已付款</li>
                    <li>交易关闭</li>
                    <li>交易完成</li>-->
                </ul>


                <div class="tab_box">
                	
                    <div class="tab_box-diva">
                    	<?php
							$order_content='';
							
							while($row1 = $result1->fetch_assoc()) {
								if($row['img_src']=="") {
									$img_src = "../cheyuzhou_bg/upload/lost.jpg";
								}
								$result2 = $conn->query("select * from ".$car_dataset." where id='".$row1['car_id']."'");
								$row2 = $result2->fetch_assoc();
								$state_content = '';//不同状态下有不同的按钮和不同的说明文字
								$state_title = '';
								$app_time ='';
								if($row1['appointment_time']!='') {
									$appointment_time = explode(',',$row1['appointment_time']);
								
								
									$app_time = $appointment_time[0].'年'.$appointment_time[1].'月'.$appointment_time[2].'日 '.$appointment_time[3];
								}
								switch($row1['state']) {
									//未处理 
									case "2":
										//标题部分
										$state_title = '<p>我们的工作人员会尽快与您联系，请保持手机畅通！</p>';
										//右侧按钮
										$state_content ='<li class="use-uc"><button type="submit" class="use-ucbutt">处理中</button><p><a href="javascript:void(0)" onclick="cancel_order(\''.$row2['id'].'\',this)">取消预约</a></p></li>';
										
										break;
									//预约成功，未付订金 
									case "4":
										$state_title = '<p><i class="seecar-time"></i>看车时间：'.$app_time.'</p><p><i class="seecar-location"></i>看车地点：</p>';
										$state_content ='<li class="use-uc"><form action="'.$SER_ADD.'/create_direct_pay_by_user-PHP-UTF-8/alipayapi.php" class="alipayform" method="post" target="_blank"><input type="hidden" name="WIDout_trade_no" id="out_trade_no" value="'.$row1['order_number'].'"><button type="submit" class="use-ucbuttb pos-left">支付定金</button></form><i class="waring"></i><p><a href="javascript:void(0)" onclick="cancel_order(\''.$row2['id'].'\',this)">取消预约</a></p><div class="prompt"><div class="popup"><span><em></em></span>目前只支持支付宝预付订金，订金可在实际购车时抵用一部分现金！</div></div></li>';
										break;
									case "7":
										$state_title = '<p><i class="seecar-time"></i>看车时间：'.$app_time.'</p><p><i class="seecar-location"></i>看车地点：</p>';
										$state_content ='<li class="use-uc"><button type="submit" class="use-ucbutt">看车中</button><p><a href="#" class="disapp">我要退款</a></p></li>';
										break;
									case "5":
										$state_content ='<li class="use-uc"><i class="deal-ok"></i></li>';
										break;
									case "6":
										$state_content ='<li class="use-uc"><i class="deal-close"></i></li>';
										break;
										
										
								}
								$order_content.='<div class="appointment"><div class="appointment-topbg"><span>订单编号：'.$row1['order_number'].'</span></div><ul><li class="use-ua"><img src="'.$img_src.'" /></li><li class="use-ub"><span><a href="'.$SER_ADD.'/buycar_detail?id='.$row2['id'].'">'.$row2['brand_name'].'</a></span>'.$state_title.'</li><li class="use-ud"><span>￥'.$row2['current_price'].'万</span></li>'.$state_content.'</ul></div>';
							}
							echo $order_content;
							
						?>
                      
                    </div>
                    <div class="buycar-pages">
					<?php
                         //显示分页
                         $page = new page($total, $showrow, $curpage, $url, 2);
                         echo $page->myde_write();
                                
                    ?>
     </div>
                
                  
                </div>
           </div>
           
       </div>
 

   
<!--底部-->
<?php
	require_once('footer.php');
?>